package com.example.teesystem.controller;

import com.example.teesystem.common.Permission.PermissionCheck;
import com.example.teesystem.common.exception.CustomException;
import com.example.teesystem.common.utils.PermissionUtil;
import com.example.teesystem.common.utils.Response;
import com.example.teesystem.common.utils.ResultCode;
import com.example.teesystem.entity.vo.*;
import com.example.teesystem.entity.vo.clazz.*;
import com.example.teesystem.filter.MyHandlerInterceptor;
import com.example.teesystem.service.ClassService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Map;

@RestController
@RequestMapping(value = "/tee/class",produces = "application/json; charset=utf-8")
@RequiredArgsConstructor
public class ClassController {
    private final ClassService classService;
    
    /**
     * 添加班级
     * @param classUploadVo 班级信息
     * @return
     */
    @PostMapping("/add")
    @PermissionCheck(PermissionUtil.CLASS_INFO_MANAGE)
    public Response add(@Valid @RequestBody(required = false) ClassUploadVo classUploadVo) {
        if(!PermissionUtil.permissionCheck(MyHandlerInterceptor.getUid(),PermissionUtil.CLASS_INFO_MANAGE,classUploadVo.getCourseId())){
            return Response.error(ResultCode.NO_PERMISSION);
        }
        if(classService.add(classUploadVo)) {
            return Response.ok();
        } else {
            throw new CustomException(ResultCode.ADD_INFO_FAIL);
        }
        
    }
    
    /**
     * 更新班级信息
     * @param classUpdateVo 班级信息
     * @return
     */
    @PostMapping("/update")
    @PermissionCheck(PermissionUtil.CLASS_INFO_MANAGE)
    public Response update(@Valid @RequestBody(required = false) ClassUpdateVo classUpdateVo) {
        if(!PermissionUtil.permissionCheck(MyHandlerInterceptor.getUid(),PermissionUtil.CLASS_INFO_MANAGE,classUpdateVo.getCourseId())){
            return Response.error(ResultCode.NO_PERMISSION);
        }
        if(classService.update(classUpdateVo)) {
            return Response.ok();
        } else {
            throw new CustomException(ResultCode.UPDATE_INFO_FAIL);
        }
    }
    
    /**
     * 条件检索
     * @param classSelectVo 条件检索
     * @return
     */
    @PostMapping("/getClasses")
    public Response getClasses(@Valid @RequestBody(required = false) ClassSelectVo classSelectVo) {
        return Response.ok(classService.getClasses(classSelectVo));
    }
    
    /**
     * 删除
     * @param classDeleteVo 删除条件
     * @return
     */
    @PostMapping("/delete")
    @PermissionCheck(PermissionUtil.CLASS_INFO_MANAGE)
    public Response delete(@Valid @RequestBody ClassDeleteVo classDeleteVo) {
        if(classService.delete(classDeleteVo)) {
            return Response.ok();
        } else {
            throw new CustomException(ResultCode.DELETE_INFO_FAIL);
        }
    }

    @PostMapping(value = "/classEvaluate")
    public Response classEvaluate(@RequestBody Map<String,Integer> map){
        int courseId;
        try{
            courseId = map.get("courseId");
        }catch (Exception e){
            return Response.error(ResultCode.PARAMETER_ERROR);
        }
        return classService.classEvaluate(courseId);
    }
}
